package leetcode.easy;

/**
 * ClassName: $70_climbStairs
 * Description:
 * date: 2021-11-04 10:32
 *
 * @author liyifan
 */
public class $70_climbStairs {
    public int climbStairs(int n) {
        if (n <= 2)
            return n;
        int[] dp = new int[n + 1];
        dp[1] = 1;
        dp[2] = 2;
        for (int i = 3;i <= n;i++) {
            dp[i] = dp[i - 1] + dp[i - 2];
        }
        return dp[n];
    }

    public int climbStairs2(int n) {
        if (n <= 2)
            return n;
        int p = 1;
        int q = 2;
        int r = p + q;
        for (int i = 3;i < n;i++) {
            p = q;
            q = r;
            r = p + q;
        }
        return r;
    }

    public int climbStairs3(int n) {
        double sqrt5 = Math.sqrt(5);
        double fibn = Math.pow((1 + sqrt5) / 2, n + 1) - Math.pow((1 - sqrt5) / 2, n + 1);
        return (int) Math.round(fibn / sqrt5);
    }

}
